package pms.common.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;

import pms.common.service.UserAuthService;
import pms.common.utils.Consts;
import pms.common.utils.PMSLog;
import pms.common.utils.StringValidator;
import pms.common.value.UserSessionValue;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Maniac
 *
 */
public class LoginAuthAction extends ActionSupport {
	
	private static final long serialVersionUID = 2276244650030995925L;
	
	private UserAuthService userAuthService;
	
	/** ユーザー名 */
	private String username = "";
	
	public UserAuthService getUserAuthService() {
		return userAuthService;
	}


	public void setUserAuthService(UserAuthService userAuthService) {
		this.userAuthService = userAuthService;
	}

	/** パスワード */
	private String password = "";
	
	public String getUsername() {
		return username;
	}

	/** 請求先URL */
	private String returnUrl = "";
	
	public String getReturnUrl() {
		return returnUrl;
	}


	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}


	public void setUsername(String username) {
		this.username = username;
	}


	public String getPassword() {
		return password;
	}


	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String execute() {
		PMSLog.start();
		
		//チェック処理
		if(StringValidator.isEmpty(username)) {
		   	addActionError(getText("validation.login.username"));
		   	PMSLog.debug("ユーザー名入力エラー発生");
			
		}
		else if(StringValidator.isEmpty(password)) {
		   	addActionError(getText("validation.login.password"));
			PMSLog.debug("パスワード入力エラー発生");
		}
		
		if(this.hasActionErrors()) {
        	PMSLog.end();
    		return INPUT;
        }
		
		//認証処理
		HttpServletRequest l_request= ServletActionContext.getRequest();
        HttpSession l_session = l_request.getSession(true);
        PMSLog.debug("username:" + username);
        PMSLog.debug("password:" + password);
        UserSessionValue l_userSessionValue = userAuthService.authUser(username, password);
        if(l_userSessionValue != null) {
        	l_session.setAttribute(Consts.USER_AUTH_KEY, username+":"+password);
        	String l_returnUrl = (String)l_session.getAttribute(Consts.RETURN_URL_KEY);
        	l_session.removeAttribute(Consts.RETURN_URL_KEY);
        	
        	l_session.setAttribute(Consts.USER_SESSION_KEY, l_userSessionValue);
        	PMSLog.debug("l_returnUrl" + l_returnUrl);
        	if(StringValidator.isEmpty(l_returnUrl)) {
        		PMSLog.end();
            	return SUCCESS;
        	} else {
        		returnUrl = l_returnUrl;
        		PMSLog.debug("リターンURL："+ returnUrl);
        		PMSLog.end();
            	return "return";
        	}
        	
        } else {
        	this.addActionError(getText("validation.login.autherror"));
        	PMSLog.end();
    		return INPUT;
        }
	}
}
